﻿using System;
using System.Windows.Forms;
using Segmentation.Business;
using System.Drawing;

namespace Segmentation.UI
{
    internal class PictureBoxPage : TabPage
    {
		#region Fields 

        private ImageContainer imageContainer;
        private PictureBox pictureBox;

		#endregion Fields 

		#region Constructors 

        private PictureBoxPage(string text)
        {
            this.pictureBox = new PictureBox();

            this.pictureBox.Location = new Point(0, 0);
            this.pictureBox.Name = "_pictureBox";
            this.pictureBox.Size = new Size(64, 64);
            this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            this.pictureBox.TabIndex = 0;
            this.pictureBox.TabStop = false;

            base.SuspendLayout();
            base.AutoScroll = true;
            base.Controls.Add(pictureBox);
            base.Location = new Point(4, 22);
            base.Name = text;
            base.Padding = new Padding(3);
            base.Size = new Size(838, 468);
            base.TabIndex = 0;
            base.Text = text;
            base.UseVisualStyleBackColor = true;
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public PictureBoxPage(string text, ImageContainer container)
            : this(text)
        {
            this.imageContainer = container;
            this.pictureBox.Image = (Bitmap)container.Bitmap.Clone();
        }

        public PictureBoxPage(string text, Bitmap bitmap)
            : this(text)
        {
            this.pictureBox.Image = bitmap;
        }

		#endregion Constructors 

		#region Properties 

        public ImageContainer ImageContainer
        {
            get 
            { 
                return imageContainer; 
            }
        }

        public Bitmap Bitmap
        {
            get
            {
                return this.pictureBox.Image as Bitmap;
            }
        }

        public bool IsImageExploited
        {
            get
            {
                return (this.imageContainer != null);
            }
        }

		#endregion Properties 

		#region Methods 

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.pictureBox.Dispose();
                if (this.imageContainer != null)
                {
                    this.imageContainer.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        public override void Refresh()
        {
            if (this.IsImageExploited)
            {
                Image image = this.pictureBox.Image;
                this.pictureBox.Image = (Bitmap)this.imageContainer.Bitmap.Clone();
                image.Dispose();
            }
            
            base.Refresh();
        }

		#endregion Methods 
    }
}
